package com.vince.hulk.mechanism
{
import com.vince.hulk.character.Character;

import org.flixel.FlxObject;
import org.flixel.FlxSprite;

/**
 * author yangwz
 */		
public class MovableBlock extends FlxSprite implements IMechanism
{
	//==========================================================================
	//  Constructor
	//==========================================================================
	/**
	 * Constructs a new <code>MovableBlock</code> instance.
	 * 
	 */
	public function MovableBlock(X:Number=0, Y:Number=0)
	{
		super(X, Y, null);
		loadGraphic(img, true, true, 80, 80);
		acceleration.y = 0;
		drag.x = FRICTION;
		immovable = true;
		lock();
	}
	//==========================================================================
	//  Variables
	//==========================================================================
	[Embed(source = "assets/MovableBlock.png")]
	private var img:Class;
	/**
	 * 重力 
	 */	
	private const GRAVITY:int = 1000;
	/**
	 * 惯性 
	 */	
	private const FRICTION:int = 350;
	
	public var isLocked:Boolean;
	
	/**
	 * 是否可以推动 
	 */	
	public var beingPushed:Boolean = false;
	//==========================================================================
	//  Methods
	//==========================================================================
	public function activate(character:Character):void
	{
		if (character.size() >= 2 && (isTouching(LEFT) || isTouching(RIGHT)))
		{
			beingPushed = true;
		}
		else
		{
			beingPushed = false;
		}
	}
	
	public function toggle():void
	{
	}
	
	/**
	 * @inheritDoc
	 */
	override public function update():void
	{
		if (beingPushed || !isTouching(FlxObject.FLOOR) && !isTouching(UP))
		{
			if (isLocked)
			{
				unLock();
			}
		}
		else if (!isLocked && isTouching(FlxObject.FLOOR))
		{
			lock();
		}
		beingPushed = false;
		super.update();
	}
	
	/**
	 * 锁定 
	 */	
	public function lock():void
	{
		isLocked = true;
		immovable = true;
		moves = false;
		velocity.x = 0;
		velocity.y = 0;
		acceleration.y = 0;
	}
	
	/**
	 * 解锁 
	 */	
	public function unLock():void
	{
		isLocked = false;
		immovable = false;
		moves = true;
		acceleration.y = GRAVITY;
	}
}
}